package com.sunnada.edu.system.pub.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
 * explain 
 * @author chs 2012-12-6
 * version 1.0
 */
public class DateInfo {
	public static String getFormatDate(Date date) {
		if (date != null) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			return sdf.format(date);
		}
		return null;
	}

	public static String getFormatDateHms(Date date) {
		if (date != null) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return sdf.format(date);
		} else {
			return "";
		}
	}

	public static String getCurrentDateHms() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String tempDate = sdf.format(new Date());
		try {
			return tempDate;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String getFormatDateHmsToNums(Date date) {
		if (date != null) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			return sdf.format(date);
		} else {
			return "";
		}

	}
	public static String getFormatDateHmsToNumss(Date date) {
		if (date != null) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
			return sdf.format(date);
		} else {
			return "";
		}

	}

	public static String getYYMM(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		return sdf.format(date);
	}

	public static String getYYMMDD(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return sdf.format(date);
	}

	public static String getHHMMSS(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
		return sdf.format(date);
	}

	public static Date toDate(String str) {
		if (str.length() < 12) {
			str = str + " 00:00:00";
		}
		try {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(str);
		} catch (ParseException e) {
			return null;
		}
	}

	public static Date toDateYMD(String str) {
		try {
			return new SimpleDateFormat("yyyy-MM-dd").parse(str);
		} catch (ParseException e) {
			return null;
		}
	}
	public static Date toDateYMDHMS(String str) {
		try {
			return new SimpleDateFormat("yyyyMMddHHmmss").parse(str);
		} catch (ParseException e) {
			return null;
		}
	}
	/**
	 * 几分钟后
	 * */
	public static Date timeLater(int s){
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MINUTE, 30); 
		return calendar.getTime();
	}
	/**
	 * 几天后
	 * @param args
	 */
	public static Date dateLater(int s){
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, s); 
		return calendar.getTime();
	}

	/**
	 * 返回与当天相隔n天的日期
	 * 
	 * @param date
	 * @param i
	 * @return
	 */
    public static Date addDay(String date,int i){
    	try {
    		Date newDate=toDateYMD(date);
    	    Calendar calendar = Calendar.getInstance();
    	    calendar.setTime(newDate);
    	    calendar.add(Calendar.DATE, i);
    	    return calendar.getTime();
		} catch (Exception e) {
			return null;
		}
      
    }
    
    public static List<String> getDatesBetweenTwoDate(String beginDate, String endDate) {    
        List<String> lDate = new ArrayList();
        Calendar cal = Calendar.getInstance();
        //使用给定的 Date 设置此 Calendar 的时间
        cal.setTime(toDateYMD(beginDate));
        boolean bContinue = true;
        while (bContinue) {
            //根据日历的规则，为给定的日历字段添加或减去指定的时间量
            cal.add(Calendar.DAY_OF_MONTH, 1);
            // 测试此日期是否在指定日期之后
            if (toDateYMD(endDate).after(cal.getTime())) {
                lDate.add(getFormatDate(cal.getTime()));
            } else {
                break;
            }
        }
        return lDate;
    }
    
	public static void main(String[] args) {
		/*Date d = toDate("2012-03-09 13:48:49.484");
		Date s=toDateYMDHMS("20150723000000");
		System.out.println(getFormatDateHms(s)); */	
		
//		System.out.println(getFormatDateHms(addDay("2015-11-25",1)));
		
		String start = "2012-01-03"; 
	    String end = "2012-01-03"; 
	    List<String> listDate = getDatesBetweenTwoDate(start, end);    
	     for(String date:listDate){    
	         System.out.println(date);    
	     } 
	}
	
	
}
